package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.DTO.SysRoleDTO;
import com.itheima.VO.MenuVO;
import com.itheima.VO.SysRoleVO;
import com.itheima.entity.SysRole;
import com.itheima.exception.RepeatNameException;
import com.itheima.json.PageBean;
import com.itheima.mapper.SysRoleMapper;
import com.itheima.mapper.SysRoleMenuMapper;
import com.itheima.service.SysMenuService;
import com.itheima.service.SysRoleMenuService;
import com.itheima.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl implements SysRoleService {

    private final SysRoleMapper sysRoleMapper;

    private final SysRoleMenuMapper sysRoleMenuMapper;

    private final SysRoleMenuService sysRoleMenuService;

    private final SysMenuService sysMenuService;

    @Override
    public PageBean page(Integer page, Integer limit, String name) {
        PageHelper.startPage(page,limit);
        List<SysRole> sysRoleList = sysRoleMapper.page(name);
        List<SysRoleVO> sysRoleVOList = listTransformToVO(sysRoleList);
        PageInfo<SysRoleVO> pageInfo = PageInfo.of(sysRoleVOList);
        PageBean pageBean = new PageBean();
        pageBean.setCount(sysRoleList.size());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setPageSize(limit);
        pageBean.setCurrentPage(page);
        pageBean.setData(pageInfo.getList());
        return pageBean;
    }


    @Override
    public List<SysRole> allData() {
        List<SysRole> sysRoles = sysRoleMapper.selectSysRoles();
        return sysRoles;
    }

    @Override
    @Transactional
    public void insertSysRole(SysRole sysRole) {
        Integer count = sysRoleMapper.selectCountByName(sysRole.getName());
        if (count > 0){
            throw new RepeatNameException("角色名重复");
        }
        sysRole.setId(UUID.randomUUID().toString());
        sysRole.setDelFlag("0");
        sysRole.setSuperadmin(0);
        sysRole.setCreateDate(LocalDateTime.now());
        sysRole.setUpdateDate(LocalDateTime.now());
        sysRoleMapper.insertSysRole(sysRole);
        List<Integer> menuids = sysRole.getMenuids();
        sysRoleMenuService.insertRoleMenu(sysRole.getId(), menuids);
    }

    @Override
    public void deleteRoleById(String id) {
        sysRoleMapper.deleteRoleById(id);
        sysRoleMenuService.deleteRoleMenuByRoleId(id);
    }

    @Override
    public SysRoleVO selectRoleById(String id) {
        SysRole sysRole = sysRoleMapper.selectRoleById(id);
        SysRoleVO sysRoleVO = transformToVO(sysRole);
        List<Integer> menuIds = sysRoleMenuService.selectRoleMenuByRoleId(id);
        sysRoleVO.setMenuids(menuIds);
        List<MenuVO> menuVOList = sysMenuService.selectMenuByMenuId(menuIds);
        sysRoleVO.setAuthList(menuVOList);
        return sysRoleVO;
    }

    @Override
    public void update(SysRole sysRole) {
        Integer count = sysRoleMapper.selectCountByNameNotSelf(sysRole.getName(),sysRole.getId());
        if (count > 0){
            throw new RepeatNameException("角色名重复");
        }
        sysRoleMapper.updateSysRole(sysRole);

        //先删除
        sysRoleMenuMapper.deleteByRoleId(sysRole.getId());

        //再新增
        sysRoleMenuMapper.insertRoleMenu(sysRole.getId(),sysRole.getMenuids());
    }


    private List<SysRoleVO> listTransformToVO(List<SysRole> sysRoleList) {
        return sysRoleList.stream().map(sysRole -> {
            SysRoleVO sysRoleVO = transformToVO(sysRole);
            return sysRoleVO;
        }).collect(Collectors.toList());
    }

    private SysRoleVO transformToVO(SysRole sysRole) {
        SysRoleVO sysRoleVO = new SysRoleVO();
        BeanUtils.copyProperties(sysRole, sysRoleVO);
        sysRoleVO.setCreate_date(sysRole.getCreateDate());
        sysRoleVO.setUpdate_date(sysRole.getUpdateDate());
        sysRoleVO.setDel_flag(sysRole.getDelFlag());
        sysRoleVO.setDefault_data(sysRole.getDefaultData());
        return sysRoleVO;
    }

}
